package cn.cloudtogo.server.mysql.controller;

import cn.cloudtogo.common.atomapi.ApiDisplay;
import cn.cloudtogo.common.atomapi.ApiListQuery;
import cn.cloudtogo.common.atomapi.ApiMethodModel;
import cn.cloudtogo.common.types.Result;
import cn.cloudtogo.server.mysql.service.ApiListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.cloudtogo.server.WebResponse.ok;

/**
 * @author yaolianhua789@gmail.com
 **/
@RestController
@RequestMapping("/api/display")
@Slf4j
public class ApiDisplayController {

    private final ApiListQuery apiListQuery;

    public ApiDisplayController(ApiListService apiListQuery) {
        this.apiListQuery = apiListQuery;
    }

    @GetMapping("/{uuid}")
    public ResponseEntity<Result<List<ApiDisplay>>> apis(@PathVariable String uuid,
                                                         @RequestParam(required = false) String method,
                                                         @RequestParam(required = false) String table) {
        List<ApiDisplay> apiModels = apiListQuery.query(uuid, method, table);
        return ok(apiModels);
    }

    @GetMapping
    public ResponseEntity<Result<ApiMethodModel>> api(@RequestParam("apiKey") String apiKey) {
        ApiMethodModel model = apiListQuery.query(apiKey);
        return ok(model);
    }


}
